/**
 * 
 */
package com.hand.cloud.b2b.daos.impl;

import de.hybris.platform.constants.GeneratedCoreConstants.Enumerations.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.servicelayer.exceptions.ModelSavingException;
import de.hybris.platform.servicelayer.model.ModelService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hand.cloud.b2b.daos.EPOrderDAO;
import com.hand.cloud.b2b.daos.ProductDAO;
import com.hand.cloud.b2b.model.EPAddressModel;
import com.hand.cloud.b2b.model.EPOrderEntryModel;
import com.hand.cloud.b2b.model.EPOrderModel;
import com.hand.cloud.b2b.model.TB_OrderModel;
import com.hand.cloud.b2b.model.TB_TradeModel;


/**
 * @author StephenWang
 * 
 */
@Component(value = "epOrderDAO")
public class DefaultEPOrderDAO implements EPOrderDAO
{

	public static Logger LOG = Logger.getLogger(DefaultEPOrderDAO.class);

	@Autowired
	private ModelService modelService;

	@Autowired
	private ProductDAO productDAO;

	/**
	 * 创建EP订单信息
	 * 
	 * @param trade
	 * @return
	 */
	@Override
	public EPOrderModel createEpOrder(final TB_TradeModel trade)
	{
		LOG.info("---------createEpOrder--------");
		final EPOrderModel epOrder = this.modelService.create(EPOrderModel.class);


		final EPAddressModel address = new EPAddressModel();
		/*
		 * address.setReceiver_address(trade.getReceiver_address()); address.setReceiver_city(trade.getReceiver_city());
		 * address.setReceiver_district(trade.getReceiver_district());
		 * address.setReceiver_mobile(trade.getReceiver_mobile()); address.setReceiver_name(trade.getReceiver_name());
		 * address.setReceiver_state(trade.getReceiver_state()); address.setReceiver_zip(trade.getReceiver_zip());
		 */

		address.setReceiverAddress(trade.getReceiver_address());
		address.setReceiverCity(trade.getReceiver_city());
		address.setReceiverDistrict(trade.getReceiver_district());
		address.setReceiverMobile(trade.getReceiver_mobile());
		address.setReceiverName(trade.getReceiver_name());
		address.setReceiverState(trade.getReceiver_state());
		address.setReceiverZip(trade.getReceiver_zip());

		//	epOrder.setBuyer_alipay_no(trade.getBuyer_alipay_no());
		epOrder.setBuyerAlipayNo(trade.getBuyer_alipay_no());
		epOrder.setCreationtime(new Date());
		epOrder.setDeliveryAddress(address);
		/*
		 * epOrder.setDiscount_fee(strToDouble(trade.getDiscount_fee()));
		 * epOrder.setHas_post_fee(trade.getHas_post_fee()); epOrder.setInvoice_name(trade.getInvoice_name());
		 * epOrder.setInvoice_type(trade.getInvoice_type());
		 */
		epOrder.setDiscountFee(strToDouble(trade.getDiscount_fee()));
		epOrder.setHasPostFee(trade.getHas_post_fee());
		epOrder.setInvoiceName(trade.getInvoice_name());
		epOrder.setInvoiceType(trade.getInvoice_type());

		epOrder.setPayment(strToDouble(trade.getPayment()));
		epOrder.setPaymentAddress(address);
		/*
		 * epOrder.setPoint_fee(trade.getPoint_fee()); epOrder.setPost_fee(strToDouble(trade.getPost_fee()));
		 * epOrder.setSeller_cod_fee(strToDouble(trade.getSeller_cod_fee()));
		 * epOrder.setSnapshot_url(trade.getSnapshot_url());
		 */
		epOrder.setPointFee(trade.getPoint_fee());
		epOrder.setPostFee(strToDouble(trade.getPost_fee()));
		epOrder.setSellerCodFee(strToDouble(trade.getSeller_cod_fee()));
		epOrder.setSnapshotUrl(trade.getSnapshot_url());

		epOrder.setStatus(trade.getStatus());
		//	epOrder.setTotal_fee(strToDouble(trade.getTotal_fee()));
		epOrder.setTotalFee(strToDouble(trade.getTotal_fee()));
		epOrder.setType(trade.getType());
		epOrder.setSourcePk(trade.getPk().toString());

		final List<EPOrderEntryModel> entries = new ArrayList<EPOrderEntryModel>();

		for (final TB_OrderModel tbOrder : trade.getOrders())
		{
			final EPOrderEntryModel entry = createEpOrderEntry(tbOrder);
			entry.setOrder(epOrder);
			try
			{
				this.modelService.save(entry);
			}
			catch (final ModelSavingException e)
			{
				e.printStackTrace();
				LOG.info("---------createEpOrder entry Error!------------");
			}
			entries.add(entry);
		}

		epOrder.setOrderEntries(entries);

		try
		{
			this.modelService.save(epOrder);
			LOG.info("EPOrder_PK:" + epOrder.getPk());
		}
		catch (final ModelSavingException e)
		{
			e.printStackTrace();
			LOG.info("---------createEpOrder Error!------------");
			return null;
		}

		return epOrder;
	}

	/**
	 * 是否为空判断
	 * 
	 * @param str
	 * @return
	 */
	private Double strToDouble(final String str)
	{
		if (str == null || "".equals(str))
		{
			return 0d;
		}

		return Double.parseDouble(str);
	}

	/**
	 * 创建订单行信息
	 * 
	 * @param tbOrder
	 * @return
	 */
	public EPOrderEntryModel createEpOrderEntry(final TB_OrderModel tbOrder)
	{
		final EPOrderEntryModel orderEntry = this.modelService.create(EPOrderEntryModel.class);

		// 获取商品信息
		//final ProductModel product = this.productDAO.getProductByPK(tbOrder.getOuter_iid());
		//		final ProductModel product = this.productDAO.getProductByPK("8796137586689");
		final ProductModel product = this.productDAO.getProductByPK("8796093054977");

		// 获取商品信息
		//final ProductModel product = this.productDAO.getProductByCode("637227");

		orderEntry.setProduct(product);
		orderEntry.setNum(tbOrder.getNum());
		orderEntry.setTotalFee(strToDouble(tbOrder.getTotal_fee()));
		//	orderEntry.setTotal_fee(strToDouble(tbOrder.getTotal_fee()));

		try
		{
			this.modelService.save(orderEntry);
		}
		catch (final ModelSavingException e)
		{
			e.printStackTrace();
			LOG.info("--------createEpOrderEntry Error!----------");
		}

		return orderEntry;
	}

	/**
	 * 修改EP订单的状态
	 * 
	 * @param order
	 * @return
	 */
	@Override
	public void changeEPOrderStatus(final OrderModel order)
	{
		final EPOrderModel epOrder = order.getEpOrder();
		epOrder.setStatus(OrderStatus.COMPLETED.toString());
	}


}
